package q59_generateMatrix;

public class Solution {
    public static void main(String[] args) {
    Solution s = new Solution();
    }

    /**
     * 跟上一题几乎一样的思路，但是是正方形，可以少一些参数
     * @param n
     * @return
     */
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int c = n;
        int mode = 0;
        int x = 0, y = 0;
        int count = 1;
        while (count <= n * n){
            if (mode % 4 == 0) {
                while (y < c){
                    res[x][y++] = count++;
                }
                y--;
                x++;
                mode++;
            }else if (mode % 4 == 1) {
                while (x < c){
                    res[x++][y] = count++;
                }
                x--;
                y--;
                mode++;
            }else if (mode % 4 == 2) {
                while (y > n - c - 1){
                    res[x][y--] = count++;
                }
                y++;
                x--;
                mode++;
            }else if (mode % 4 == 3) {
                while (x > n - c){
                    res[x--][y] = count++;
                }
                x++;
                y++;
                c--;
                mode++;
            }

        }
        return res;
    }

}
